package com.dayi.activity.coachstudy;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.dayi.R;
import com.dayi.R2;
import com.dayi.bean.coachstudy.MainInfoBean_1;
import com.lib.app.ARouterPathUtils;
import com.lib.app.CodeUtil;
import com.lib.fastkit.db.shared_prefrences.SharedPreferenceManager;
import com.lib.fastkit.http.ok.HttpUtils;
import com.lib.fastkit.utils.share.tool.StringUtil;
import com.lib.fastkit.utils.time_deal.TimeUtils;
import com.lib.fastkit.utils.timer_countdown.CountDownTimer;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.http.call_back.HttpNormalCallBack;
import com.lib.observer.ObserverListener;
import com.lib.observer.ObserverManager;
import com.lib.ui.activity.BaseAppActivity;
import com.lib.ui.adapter.BaseAdapter;
import com.lib.utls.glide.GlideConfig;
import com.lib.view.navigationbar.NomalNavigationBar;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

@Route(path = ARouterPathUtils.Tablet_DaYi_CoachWaitAnswerActivity)
public class CoachWaitAnswerActivity extends BaseAppActivity implements ObserverListener {

    @BindView(R2.id.iv_gif)
    ImageView ivGif;
    @BindView(R2.id.tv_cut_time)
    TextView tvCutTime;
    @BindView(R2.id.btn_back)
    Button btnBack;
    @BindView(R2.id.rv)
    RecyclerView rvVideo;

    ShortVideoInfoAdapter videoAdapter;
    List<MainInfoBean_1.ObjBean.TeacherShortVideoBean> videoList = new ArrayList<>();

    @Override
    protected void onCreateView() {

        initTitle();

        initMainInfoData();

        initGif();

        videoAdapter = new ShortVideoInfoAdapter(this, videoList);
        MyLinearLayoutManager manager2 = new MyLinearLayoutManager(this, MyLinearLayoutManager.HORIZONTAL, false);
        rvVideo.setAdapter(videoAdapter);
        rvVideo.setLayoutManager(manager2);


        ObserverManager.getInstance().add(this);

    }

    protected void initTitle() {
        NomalNavigationBar navigationBar = new
                NomalNavigationBar.Builder(this)
                .setTitle("等待老师辅导")
                .builder();

        // navigationBar.getRightTextView()
    }

    private void initGif() {
        Glide.with(this).load(R.drawable.wait_upload).listener(new RequestListener() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) {
                return false;
            }

            @Override
            public boolean onResourceReady(Object resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) {
                if (resource instanceof GifDrawable) {
                    //加载一次
                    ((GifDrawable) resource).setLoopCount(-1);
                }

                return false;
            }


        }).into(ivGif);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_study_waitanswer;
    }

    private void initMainInfoData() {

        HttpUtils.with(this)
                .addParam("requestType", "TABLET_HOME_DATA")
                .addParam("token", SharedPreferenceManager.getInstance(this).getUserCache().getUserToken())
                .execute(new HttpNormalCallBack<MainInfoBean_1>() {
                    @Override
                    public void onSuccess(MainInfoBean_1 result) {

                        if (CodeUtil.CODE_200 == result.getCode()) {
                            if (result.getObj() != null) {

                                if (result.getObj().getTeacher_short_video() != null) {
//                                    initShortVideoInfo(result.getObj().getTeacher_short_video());
                                    if (videoList.size() > 0) {
                                        videoList.clear();
                                    }
                                    videoList.addAll(result.getObj().getTeacher_short_video());
                                    videoAdapter.notifyDataSetChanged();
                                }
                            }
                        }
                    }

                    @Override
                    public void onError(String e) {

                    }
                });
    }

    @Override
    public void observerUpData(String content) {

        if (StringUtil.isNotEmpty(content, true)) {
            //用户在推送弹出框点击了确认，将退出本界面
            if ("CloseWaitActivity".equals(content)) {
                finish();
            }
        }
    }


    class ShortVideoInfoAdapter extends BaseAdapter<MainInfoBean_1.ObjBean.TeacherShortVideoBean> {

        public ShortVideoInfoAdapter(Context context, List<MainInfoBean_1.ObjBean.TeacherShortVideoBean> mData) {
            super(context, mData);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_video;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, int position, List<MainInfoBean_1.ObjBean.TeacherShortVideoBean> mData) {

            FrameLayout linVideo = holder.getView(R.id.fr_video);
//            int screenWidth = DisplayUtil.getScreenWidth(getActivity());
//            ViewGroup.LayoutParams params = linVideo.getLayoutParams();
//            params.width = screenWidth / 6;
//            linVideo.setLayoutParams(params);

            final MainInfoBean_1.ObjBean.TeacherShortVideoBean teacherVideoBean = mData.get(position);

            ImageView ivUser = holder.getView(R.id.iv_user);
            if (StringUtil.isNotEmpty(mData.get(position).getPhoto_url(), true)) {
                Glide.with(CoachWaitAnswerActivity.this)
                        .load(mData.get(position).getPhoto_url())
                        .apply(GlideConfig.getCircleOptions())
                        .into(ivUser);
            }

            ivUser.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    ARouter.getInstance().build(ARouterPathUtils.Tablet_User_InfoActivity)
                            .withInt("teacherId", teacherVideoBean.getUser_id()).navigation();
                }
            });

            if (StringUtil.isNotEmpty(teacherVideoBean.getUser_name(), true)) {
                holder.setText(R.id.tv_video_username, teacherVideoBean.getUser_name());
            }
            if (StringUtil.isNotEmpty(teacherVideoBean.getPublish_date(), true)) {
                holder.setText(R.id.tv_video_usertime, teacherVideoBean.getPublish_date());
            }
            if (StringUtil.isNotEmpty(teacherVideoBean.getDynamic_content(), true)) {
                holder.setText(R.id.tv_video_content, teacherVideoBean.getDynamic_content());
            }

            //关注状态
            TextView tvCare = holder.getView(R.id.tv_video_care);
            tvCare.setVisibility(View.GONE);
//            if (1 == teacherVideoBean.getAttention_status()) {
//                tvCare.setText("已关注");
//                tvCare.setBackgroundResource(R.drawable.bg_ran_cared_r_15);
//                tvCare.setTextColor(getResources().getColor(R.color.newshare_care));
//            }
//            tvCare.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//
//                    //传递关注/取关到服务器
////                    sendCareStateToService(tvCare, teacherVideoBean.getUser_id());
//                }
//            });

            ImageView ivVideoBg = holder.getView(R.id.iv_video);
            if (StringUtil.isNotEmpty(teacherVideoBean.getDynamic_hash_icon(), true)) {
                Glide.with(CoachWaitAnswerActivity.this)
                        .load(teacherVideoBean.getDynamic_hash_icon())
                        .apply(GlideConfig.getRoundOptions(10))
                        .into(ivVideoBg);
            }

            holder.setOnClickListener(R.id.lin_info, new View.OnClickListener() {
                @Override
                public void onClick(View v) {

//                    ARouter.getInstance().build(ARouterPathUtils.Tablet_User_InfoActivity)
//                            .withInt("teacherId", teacherVideoBean.getUser_id()).navigation();
                }
            });

            linVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int teacher_id = teacherVideoBean.getUser_id();
                    int dynamic_id = teacherVideoBean.getDynamic_id();
                    ARouter.getInstance().build(ARouterPathUtils.Tablet_Video_ShortVideoDetailActivity)
                            .withInt("teacher_id", teacher_id)
                            .withInt("dynamic_id", dynamic_id).navigation();

//                    int recordId = mData.get(position).getId();
//                    ARouter.getInstance().build(ARouterPathUtils.Tablet_Video_RecordDetailActivity)
//                            .withInt("recordId", recordId).navigation();

                }
            });

        }
    }

    //-----------------------------------------------------------------------------记时器

    String timeString = "";
    CountDownTimer timer = new CountDownTimer(1000 * 60 * 5, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            timeString = TimeUtils.converLongTimeToStr(millisUntilFinished);
            tvCutTime.setText("请耐心等待:" + timeString);
        }

        @Override
        public void onFinish() {

            timeString = "00:00";
            tvCutTime.setText("老师已超时，可以选择下面的老师哦！");
        }
    };


    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer.cancel();

        ObserverManager.getInstance().remove(this);

    }

}
